Return to doc.sitecore.com

Valid for Sitecore 5.3
Can I get the DataSource set and Parameters for a sublayout via code-behind?

Click here to view the version of this FAQ entry for Sitecore 5.2.

Q: 

Can I get the DataSource set and Parameters for a sublayout via code-behind?

A: 

  1. Set a certain layout and sublayout for an item with Parameters and DataSource:
    /upload/sdn5/faq/api/get sublayout parameters/get sublayout parameters.png
  2. Publish.
  3. Use this code-behind code:

1.  Codebehind

Codebehind
using Sitecore;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Data.Query;
using Sitecore.Configuration;
using Sitecore.Express;
using Sitecore.Layouts;
using Sitecore.SecurityModel;
using Sitecore.Sites;
...
      
protected void GetDataSource_Click(object sender, EventArgs e)
      {
         Database db
= Sitecore.Context.Database;
         Item item
= db.Items["/sitecore/templates/SC Printers/Section"];
        
string rend = item.Fields["__renderings"].Value;
         LayoutDefinition layout
= LayoutDefinition.Parse(rend);
         DeviceItem dev
= Sitecore.Context.Device;
         DeviceDefinition device
= layout.GetDevice(dev.ID.ToString());
         Item mySublayout
= db.Items["/sitecore/layout/Sublayouts/Two columns"];
         RenderingDefinition rendering
= device.GetRendering(mySublayout.ID.ToString());
         Response.Write(
"Parameters: " + rendering.Parameters);
         Response.Write(
"<br/>");
         Response.Write(
"DataSource: " + rendering.Datasource);
      }